<?php

namespace Admin\Model;
use Common\Model\TagModel as BashModel;
use Admin\Model\TagLinkModel;
use Admin\Model\LogInfoModel;

/**
 * 标签表，包含有所有的标签
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2018-04-23 17:23:09
 */
class TagModel extends BashModel{
    protected $link = null;
    protected $log = null;


    /**
     * 获取文件链接表
     * 
     * @return type
     */
    public function get_link(){
        if($this->link){
            return $this->link;
        }
        $this->link = new TagLinkModel();
        return $this->link;
    }
    
    /**
     * 根据target_id和target_name获取相关联的tag
     *      获取关联的tag标签
     * @param type $table_name
     * @param type $table_id
     * @param type $type 要获取的tag类型，C('type_config.TagType')的值
     * @return type
     */
    public function get_tag_link($table_name = '',$table_id = '',$type = false){
        $list = $this->get_link()->where(array('target_name'=> $this->get_table_id($table_name),'target_id'=>$table_id))->getList(0,999);
        $obj = array();
        foreach($list as $k => $v){
            $temp = $this->get_info($v['file_id']);
            if($type){
                // 如果查询出来的tag标签和传入的type类型不符合，则直接跳过本次循环
                if($temp['type'] != get_config_id('TagType',$type)){
                    continue;
                }
            }
            $v['tag_info'] = $temp;
            $obj[] = $v;
        }
        return $obj;
    }
    
    /**
     * 根据file_id和target_name获取相关联的tag
     *          获取tag关联的目标列表
     * @param type $table_name
     * @param type $file_id
     * @return type
     */
    public function get_tag_link_by_fileid($table_name = '',$file_id = ''){
        $list = $this->get_link()->where(array('target_name'=> $this->get_table_id($table_name),'file_id'=>$file_id))->getList(0,999);
        foreach($list as $k => $v){
            $list[$k]['tag_info'] = $this->get_info($v['file_id']);
        }
        return $list;
    }
    
    /**
     * 获取日志详情表
     * @return type
     */
    public function get_loginfo(){
        if($this->log){
            return $this->log;
        }
        $this->log = new LogInfoModel();
        return $this->log;
    }

    /**
     * 获取表编号
     * 
     * @param type $tab
     * @return type
     */
    public function get_table_id($tab = ''){
        return $this->get_loginfo()->get_id($tab);
    }
    
    /**
     * 将连接标记为删除状态
     * 
     * @param type $link_id
     * @return boolean
     */
    public function set_delete_status($link_id = 0,$status = 97){
        if($link_id <= 0){
            return FALSE;
        }
        return $this
                ->get_link()
                ->where(array('id'=>$link_id,'status' => array('neq',98)))
                ->save(array('status'=>$status,'del_time'=>NOW_TIME));
    }
    
    /**
     * 提交删除状态
     * 
     * @param type $target_name 表名
     * @param type $target_id 表编号
     * @return boolean
     */
    public function set_delete_submit($target_name = '',$target_id = 0,$tag_id = false){
        if(empty($target_name) || $target_id <= 0){
            return FALSE;
        }
        if($tag_id){
            return $this
                    ->get_link()
                    ->where(array(
                        'status' => 97,
                        'target_name' => $this->get_table_id($target_name),
                        'file_id' => $target_id
                    ))
                    ->save(array('status'=>98,'del_time'=>NOW_TIME));
        }
        return $this
                ->get_link()
                ->where(array(
                    'status' => 97,
                    'target_name' => $this->get_table_id($target_name),
                    'target_id' => $target_id
                ))
                ->save(array('status'=>98,'del_time'=>NOW_TIME));
    }
    
    /**
     * 回滚删除状态
     * 
     * @param type $target_name
     * @param type $target_id
     * @return boolean
     */
    public function set_delete_reback($target_name = '',$target_id = 0,$tag_id = false){
        if(empty($target_name) || $target_id <= 0){
            return FALSE;
        }
        if($tag_id){
            return $this
                    ->get_link()
                    ->where(array(
                        'status' => 97,
                        'target_name' => $this->get_table_id($target_name),
                        'file_id' => $target_id
                    ))
                    ->save(array('status'=>99,'del_time'=>0));
        }
        return $this
                ->get_link()
                ->where(array(
                    'status' => 97,
                    'target_name' => $this->get_table_id($target_name),
                    'target_id' => $target_id
                ))
                ->save(array('status'=>99,'del_time'=>0));
    }
    
    /**
     * 将连接标记为删除状态
     * 
     * @param type $link_id
     * @return boolean
     */
    public function set_delete_status_tag($tag_id = 0){
        if($tag_id <= 0){
            return FALSE;
        }
        return $this
                ->where(array('id'=>$tag_id,'status' => array('neq',98)))
                ->save(array('status'=>97,'del_time'=>NOW_TIME));
    }
    
    /**
     * 提交删除状态
     * 
     * @param type $target_name 表名
     * @param type $target_id 表编号
     * @return boolean
     */
    public function set_delete_submit_tag(){
        return $this
                ->where(array(
                    'status' => 97
                ))
                ->save(array('status'=>98,'del_time'=>NOW_TIME));
    }
    
    /**
     * 回滚删除状态
     * 
     * @param type $target_name
     * @param type $target_id
     * @return boolean
     */
    public function set_delete_reback_tag(){
        return $this
                ->where(array(
                    'status' => 97
                ))
                ->save(array('status'=>99,'del_time'=>0));
    }
}